/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.base.ws;

import com.timesoft.common.bean.ResponseCommon;
import com.timesoft.common.convertor.Base64ConvertorUtils;
import com.timesoft.hibernate.bean.Customer;
import com.timesoft.hibernate.bean.InformationSchemaColumns;
import com.timesoft.hibernate.core.CustomerCore;
import com.timesoft.hibernate.core.InformationSchemaColumnsCore;
import com.timesoft.webservice.client.base.BaseWebService;
import javax.jws.WebService;
import org.apache.log4j.Logger;

/**
 *
 * @author sorasaks
 */
@WebService(targetNamespace = "http://base.timesoft.com/",
        endpointInterface = "com.timesoft.webservice.client.base.BaseWebService",
        serviceName = "BaseWs")
public class BaseWs implements BaseWebService {

    private static final Logger LOG = Logger.getLogger(BaseWs.class);
    private static final Base64ConvertorUtils base64 = new Base64ConvertorUtils();

    @Override
    public String customerCoreGetCustomerById(String id, String isActive) throws Exception {
        ResponseCommon common = new ResponseCommon();
        try {
            Long objId = (Long) base64.stringToGenericObj(id);
            Boolean objIsActive = (Boolean) base64.stringToGenericObj(isActive);
            Customer customer = CustomerCore.getCustomerById(objId, objIsActive);
            common.setValue(base64.genericObjToString(customer));
            common.setFlag(Boolean.TRUE);
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            common.setValue(null);
            common.setFlag(Boolean.FALSE);
            common.setMessage(ex.getMessage());
        }

        return base64.genericObjToString(common);
    }

    @Override
    public String customerCoreGetCustomerByCustomerCode(String customerCode, String isActive) throws Exception {
        ResponseCommon common = new ResponseCommon();
        try {
            String objCustomerCode = (String) base64.stringToGenericObj(customerCode);
            Boolean objIsActive = (Boolean) base64.stringToGenericObj(isActive);
            Customer customer = CustomerCore.getCustomerByCustomerCode(objCustomerCode, objIsActive);
            common.setValue(base64.genericObjToString(customer));
            common.setFlag(Boolean.TRUE);
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            common.setValue(null);
            common.setFlag(Boolean.FALSE);
            common.setMessage(ex.getMessage());
        }

        return base64.genericObjToString(common);
    }

    @Override
    public String customerCoreGetCustomerByNameAndLastName(String name, String lastname, String isActive) throws Exception {
        ResponseCommon common = new ResponseCommon();
        try {
            String objName = (String) base64.stringToGenericObj(name);
            String objLastname = (String) base64.stringToGenericObj(lastname);
            Boolean objIsActive = (Boolean) base64.stringToGenericObj(isActive);
            Customer customer = CustomerCore.getCustomerByNameAndLastName(objName, objLastname, objIsActive);
            common.setValue(base64.genericObjToString(customer));
            common.setFlag(Boolean.TRUE);
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            common.setValue(null);
            common.setFlag(Boolean.FALSE);
            common.setMessage(ex.getMessage());
        }

        return base64.genericObjToString(common);
    }

    @Override
    public String customerCoreGetCustomerByEmail(String email, String isActive) throws Exception {
        ResponseCommon common = new ResponseCommon();
        try {
            String objEmail = (String) base64.stringToGenericObj(email);
            Boolean objIsActive = (Boolean) base64.stringToGenericObj(isActive);
            Customer customer = CustomerCore.getCustomerByEmail(objEmail, objIsActive);
            common.setValue(base64.genericObjToString(customer));
            common.setFlag(Boolean.TRUE);
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            common.setValue(null);
            common.setFlag(Boolean.FALSE);
            common.setMessage(ex.getMessage());
        }

        return base64.genericObjToString(common);
    }

    @Override
    public String customerCoreGetCustomerByEmailAndPassword(String email,
            String password,
            String isActive) throws Exception {
        ResponseCommon common = new ResponseCommon();
        try {
            String objEmail = (String) base64.stringToGenericObj(email);
            String objPassword = (String) base64.stringToGenericObj(password);
            Boolean objIsActive = (Boolean) base64.stringToGenericObj(isActive);
            Customer customer = CustomerCore.getCustomerByEmailAndPassword(objEmail, objPassword, objIsActive);
            common.setValue(base64.genericObjToString(customer));
            common.setFlag(Boolean.TRUE);
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            common.setValue(null);
            common.setFlag(Boolean.FALSE);
            common.setMessage(ex.getMessage());
        }

        return base64.genericObjToString(common);
    }

    @Override
    public String customerCoreGetLastCustomerByCustomerCode() throws Exception {
        ResponseCommon common = new ResponseCommon();
        try {
            Customer customer = CustomerCore.getLastCustomerByCustomerCode();
            common.setValue(base64.genericObjToString(customer));
            common.setFlag(Boolean.TRUE);
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            common.setValue(null);
            common.setFlag(Boolean.FALSE);
            common.setMessage(ex.getMessage());
        }

        return base64.genericObjToString(common);
    }

    @Override
    public String customerCoreUpdateActiveCustomer(String customer, String userName) throws Exception {
        ResponseCommon common = new ResponseCommon();
        try {
            Customer objCustomer = (Customer) base64.stringToGenericObj(customer);
            String objUsername = (String) base64.stringToGenericObj(userName);
            Boolean isSave = CustomerCore.updateActiveCustomer(objCustomer, objUsername);
            common.setValue(base64.genericObjToString(isSave));
            common.setFlag(Boolean.TRUE);
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            common.setFlag(Boolean.FALSE);
            common.setMessage(ex.getMessage());
        }

        return base64.genericObjToString(common);
    }

    @Override
    public String customerCoreSave(String customer, String userName) throws Exception {
        ResponseCommon common = new ResponseCommon();
        try {
            Customer objCustomer = (Customer) base64.stringToGenericObj(customer);
            String objUsername = (String) base64.stringToGenericObj(userName);
            Boolean isSave = CustomerCore.save(objCustomer, objUsername);
            common.setValue(base64.genericObjToString(isSave));
            common.setFlag(Boolean.TRUE);
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            common.setFlag(Boolean.FALSE);
            common.setMessage(ex.getMessage());
        }

        return base64.genericObjToString(common);
    }

    @Override
    public String informationSchemaColumnsCoreGetCharecterMaximumLengthByTableNameAndColumnName(String tableName,
            String columnName) throws Exception {
        ResponseCommon common = new ResponseCommon();
        try {
            String objTableName = (String) base64.stringToGenericObj(tableName);
            String objColumnName = (String) base64.stringToGenericObj(columnName);
            InformationSchemaColumns result = InformationSchemaColumnsCore.getCharecterMaximumLengthByTableNameAndColumnName(objTableName, objColumnName);
            common.setValue(base64.genericObjToString(result));
            common.setFlag(Boolean.TRUE);
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            common.setValue(null);
            common.setFlag(Boolean.FALSE);
            common.setMessage(ex.getMessage());
        }

        return base64.genericObjToString(common);
    }

}
